<?php

class AgencyStaffAction extends fvAction {
    
    function __construct () {
        parent::__construct(fvSite::$Layoult);
    }

    function executeIndex() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
    
    function executeEdit() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeEditdir() {
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }
    
    function executeSave() {
        $request = fvRequest::getInstance();
        
        if (!$AgencyStaff = AgencyStaffManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $AgencyStaff = new AgencyStaff();
        }
        
        if (!$Manager = ManagerManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Manager = new Manager();
        }
        $m_u = $request->getRequestParameter('m_u');
        $m = $request->getRequestParameter('m');
        $m['agency_id'] = $m_u['agency_id'];
        $m['active'] = 1;
        if (!$Manager->isNew() && (strlen($m['password']) == 0)) {
            unset($m['password']);
            unset($m['password1']);
        }
        
        $Manager->updateFromRequest($m);
        
        $Manager->save();

        $AgencyStaff->updateFromRequest($m_u);
        
        if ($AgencyStaff->save()) {
            fvResponce::getInstance()->setHeader('Id', $AgencyStaff->getPk());
            $this->setFlash("Данные успешно сохранены", self::$FLASH_SUCCESS);
        } else { 
            fvResponce::getInstance()->setHeader('X-JSON', json_encode($AgencyStaff->getValidationResult()));
            $this->setFlash("Ошибка при сохранении данных проверте правильность введенных данных", self::$FLASH_ERROR);
        }
        if ($request->getRequestParameter('redirect')) {
            fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        }        
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_AJAX_CALL;
        else return self::$FV_OK;
    }    

    function executeDelete() {
        $request = fvRequest::getInstance();
        if (!$AgencyStaff = AgencyStaffManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $this->setFlash("Ошибка при удалении.", self::$FLASH_ERROR);
        } else {
            $AgencyStaff->delete();
            $this->setFlash("Данные успешно удалены", self::$FLASH_SUCCESS);
        }
        
        fvResponce::getInstance()->setHeader('redirect', fvSite::$fvConfig->get('dir_web_root') . $request->getRequestParameter('module') . "/");
        if (fvRequest::getInstance()->isXmlHttpRequest())
            return self::$FV_NO_LAYOULT;
        else return self::$FV_OK;
    }
}

?>
